package com.system.elastic.controller;


import com.system.elastic.contant.StatusCode;
import com.system.elastic.goods.feign.GoodFeign;
import com.system.elastic.model.SkuInfo;
import com.system.elastic.service.SkuInfoService;
import com.system.elastic.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
public class ElasticController {

    @Autowired
    GoodFeign  goodFeign;
    @Autowired
    SkuInfoService skuInfoService;

    @RequestMapping("getNumebr")
    public Integer getNumber(){
        System.out.println("elastic");
        return goodFeign.count();
    }


    /***
     * 所有记录导入到搜索引擎中
     */
    @GetMapping(value = "/all/add")
    public Result addAll(){
        try {
            //添加数据到索引库中
            skuInfoService.addAll();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return new Result(true, StatusCode.OK,"导入所有数据到索引库成功！");
    }
    @GetMapping
    public Page search(@RequestParam(required = false) Map<String,String> searchMap){
        searchMap.put("startTime","20240019120000");
        if(searchMap==null){
            return null;
        }
        return skuInfoService.searchPage(searchMap);
    }
    @PostMapping("modify/{type}")
    public void modify(@RequestBody SkuInfo skuInfo, @PathVariable String type){
        skuInfoService.modify(Integer.valueOf(type),skuInfo);
    }
}
